#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ======================================#
# @Author   : duanqizhong
# @time     : 2020/6/21 20:48
# @file     : common_func.py
# @Software : PyCharm
# ======================================#
from apps.models.dboperator import query


##### env #####
def env_database_info(env):
    """
    环境用到的数据库
    """
    infos = {
        "status": True,
        "data": [],
        "msg": "ok"
    }

    sql_data = [env]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where env=%s and parameter in ('db_host','db_port')"
    sql_app = "select env,app_id,app_name as tag,parameter,value,create_time,update_time,status from env_app_var where env=%s and parameter in ('db_host','db_port')"
    business_data = query(sql_business, sql_data, fetchone=False)
    app_data = query(sql_app, sql_data, fetchone=False)
    infos["data"].extend(business_data["data"])
    infos["data"].extend(app_data["data"])
    return infos


def env_host_info(env):
    """环境用到的主机"""
    infos = {
        "status": True,
        "data": [],
        "msg": "ok"
    }
    sql_data = [env]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where env=%s and parameter='host'"
    sql_app = "select env,app_id,app_name as tag,parameter,value,create_time,update_time,status from env_app_var where env=%s and parameter='host'"
    business_data = query(sql_business, sql_data, fetchone=False)
    app_data = query(sql_app, sql_data, fetchone=False)
    infos["data"].extend(business_data["data"])
    infos["data"].extend(app_data["data"])
    return infos


def env_all_business_var_info(env):
    """主要获取业务线公共参数：不获取app参数"""
    sql_data = [env]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where env in (%s,'common')"
    infos = query(sql_business, sql_data, fetchone=False)
    return infos


##### business #####
def env_business_database_info(env, business):
    """
    环境业务用到的数据库
    """
    infos = {
        "status": True,
        "data": [],
        "msg": "ok"
    }

    sql_data = [env, business]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where env=%s and parameter='db_host' and business in (%s,'common')"
    business_data = query(sql_business, sql_data, fetchone=False)
    infos["data"].extend(business_data["data"])
    return infos


def env_business_host_info(env, business):
    """环境用到的主机"""
    infos = {
        "status": True,
        "data": [],
        "msg": "ok"
    }
    sql_data = [env, business]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where env=%s and parameter='host' and business in (%s,'common')"
    business_data = query(sql_business, sql_data, fetchone=False)
    infos["data"].extend(business_data["data"])
    return infos


def env_business_var_info(env, business):
    """主要获取业务线公共参数：不获取app参数"""
    sql_data = [env, business]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where (env=%s or env='common') and business in (%s,'common')"
    infos = query(sql_business, sql_data, fetchone=False)
    return infos


##### app info #####
def env_app_var_info(env, app):
    """环境应用信息"""
    sql_data = [env, app, app]
    env_business_var = env_all_business_var_info(env)
    sql_app = "select env,app_id,app_name as tag,parameter,value,create_time,update_time,status from env_app_var where env in (%s,'common') and (app_id=%s or app_name=%s)"
    env_app_var = query(sql_app, sql_data, fetchone=False)
    app_data = [app, app]
    sql_app_info = "select * from app where id=%s or name=%s"
    app_var = query(sql_app_info, app_data, fetchone=True)
    status = app_var["data"].get("status")
    translate_key_value = [{"env": env, "tag": "app", "parameter": key, "value": value, "status": status} for
                           key, value in app_var["data"].items()]
    infos = {
        "status": True,
        "data": translate_key_value,
        "msg": "ok"
    }
    infos["data"].extend(env_app_var["data"])
    infos["data"].extend(env_business_var["data"])
    return infos


##### single info #####
def all_business(business):
    sql_data = [business]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where business=%s"
    infos = query(sql_business, sql_data, fetchone=False)
    return infos


def all_host(host):
    sql_data = [host]
    sql_business = "select env,business as tag,parameter,value,create_time,update_time,status from env_business_var where parameter='host' and value=%s"
    business_var = query(sql_business, sql_data, fetchone=False)
    sql_app = "select env,app_id,app_name as tag,parameter,value,create_time,update_time,status from env_app_var where parameter='host' and value=%s"
    app_var = query(sql_app, sql_data, fetchone=False)
    infos = {
        "status": True,
        "data": [],
        "msg": "ok"
    }
    infos["data"].extend(business_var["data"])
    infos["data"].extend(app_var["data"])
    return infos


def all_app(app):
    infos = {
        "status": True,
        "data": [],
        "msg": "ok"
    }
    app_data = [app, app]
    sql_app_info = "select id,business,name from app where id=%s or name=%s"
    app_var = query(sql_app_info, app_data, fetchone=True)
    business = app_var["data"].get("business")
    if not business:
        return infos
    business_data = [business]
    sql_business = "select env,business as tag,parameter,parameter,value,create_time,update_time,status from env_business_var where business in (%s,'common') and parameter='host'"
    business_var = query(sql_business, business_data, fetchone=False)
    sql_app = "select env,app_id,app_name as tag,parameter,value,create_time,update_time,status from env_app_var where (app_id=%s or app_name=%s)"
    app_var = query(sql_app, app_data, fetchone=False)
    infos["data"].extend(business_var["data"])
    infos["data"].extend(app_var["data"])
    return infos


def get_env_app_port(env):
    if env:
        env_data = [env]
        env_sql = "select env.env,app.name,ebv.value as host,app.port from env left join env_business_var ebv on env.env=ebv.env left join app on app.business=ebv.business where env.status=1 and ebv.status=1 and ebv.parameter='host' and app.status=1 and env.env=%s"
        app_host_port_sql = "select env,app_name as name,parameter,value from env_app_var where status=1 and parameter in ('host','port') and env=%s"
    else:
        env_data = []
        env_sql = "select env.env,app.name,ebv.value as host,app.port from env left join env_business_var ebv on env.env=ebv.env left join app on app.business=ebv.business where env.status=1 and ebv.status=1 and ebv.parameter='host' and app.status=1"
        app_host_port_sql = "select env,app_name as name,parameter,value from env_app_var where status=1 and parameter in ('host','port')"

    env_info = query(env_sql, env_data, fetchone=False)
    app_host_port_info = query(app_host_port_sql, env_data, fetchone=False)
    # 更新app_var host port
    for row in app_host_port_info.get("data"):
        env = row.get("env")
        app = row.get("name")
        parameter = row.get("parameter")
        value = row.get("value")
        for r in env_info.get("data"):
            if r.get("env") == env and r.get("name") == app:
                if parameter == "host":
                    r["host"] = value
                else:
                    r["port"] = value
                break
    return env_info
